package com.uh.microservice.core.spring.di;


import akka.actor.Actor;
import akka.actor.IndirectActorProducer;
import org.springframework.context.ApplicationContext;

/**
 * akka的actor生产者
 * Created by liming on 2017/9/15.
 */
public class SpringActorProducer implements IndirectActorProducer {

    final private ApplicationContext applicationContext;
    final private String actorBeanName;
    private Object[] args;

    /**
     *  通过容器上下文、名称等的构造函数
     */
    public SpringActorProducer(ApplicationContext applicationContext1, String actorBeanName) {
        this.applicationContext = applicationContext1;
        this.actorBeanName = actorBeanName;
    }

    /**
     *  通过容器上下文、名称和其他参数等的构造函数
     */
    public SpringActorProducer(ApplicationContext applicationContext1, String actorBeanName, Object... args) {
        this.applicationContext = applicationContext1;
        this.actorBeanName = actorBeanName;
        this.args=args;
    }

    /**
     *  生产一个actor
     */
    public Actor produce() {
        if(args==null)
            return (Actor) applicationContext.getBean(actorBeanName);
        else
            return (Actor) applicationContext.getBean(actorBeanName,args);
    }

    /**
     *  判定actor的class
     */
    public Class<? extends Actor> actorClass() {
        return (Class<? extends Actor>) applicationContext.getType(actorBeanName);
    }
}